/**
 * 
 */
package com.example.demo;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Date;

import javax.security.auth.x500.X500Principal;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.x509.X509V1CertificateGenerator;

/**
 * @author annvcit
 * @full_name Nguyen Van Chuc An
 * @date_created Dec 19, 2014
 * @date_modified Dec 19, 2014
 * @email annvcit@gmail.com
 */
public class DemoAddProvider {
	public static void main(String[] args) throws Exception{
		Security.addProvider(new BouncyCastleProvider());
		X509V1CertificateGenerator v1CertGen = new X509V1CertificateGenerator();
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
		keyGen.initialize(1024);
		KeyPair pair = keyGen.generateKeyPair();
		PrivateKey priKey = pair.getPrivate();
		PublicKey pubKey = pair.getPublic();
		
		String issuer = "C=VN, O=NongLam, OU=NongLamCert";
		String subject = "C=VN, O=NongLam, OU=NongLamCert";
		
		v1CertGen.setSerialNumber(BigInteger.valueOf(10));
		v1CertGen.setIssuerDN(new X500Principal(issuer));
		v1CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
		v1CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 30)));
		v1CertGen.setSubjectDN(new X500Principal(subject));
		v1CertGen.setPublicKey(pubKey);
		v1CertGen.setSignatureAlgorithm("SHA1WithRSAEncryption");
		
		X509Certificate cert = v1CertGen.generateX509Certificate(priKey);
		cert.checkValidity(new Date());
		cert.verify(pubKey);
		
		
		
	}
}
